package com.tiger.cloud.controller.crm;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.tiger.cloud.common.mybatis.BaseController;
import com.tiger.cloud.common.qiniu.MagicUploader;
import com.tiger.cloud.util.Response;
import com.tiger.cloud.util.ResponseUtil;

import io.swagger.annotations.Api;

@Controller
@RequestMapping("file")
@Api(value = "文件上传",tags = "文件")
public class FileController extends BaseController {

	@Autowired
	private MagicUploader magicUploader;

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	@ResponseBody
	public Response upload(MultipartFile imageFile) throws IOException {
		String urlString =magicUploader.send(imageFile.getOriginalFilename(),imageFile.getBytes());
		System.out.println("urlString[" + urlString + "]");
		return ResponseUtil.ok(urlString);
	}

	@RequestMapping(value = "uploadMuti", method = RequestMethod.POST)
	@ResponseBody
	public Response uploadMuti(MultipartHttpServletRequest iamgefile) throws IOException {
		Map<String, MultipartFile> map = iamgefile.getFileMap();
		Map<String, String> data=new HashMap<String, String>();
		for (String filename : map.keySet()) {
			MultipartFile multipartFile = map.get(filename);
			if (multipartFile != null) {
				String urlString =magicUploader.send(multipartFile.getOriginalFilename(),multipartFile.getBytes());
				data.put(multipartFile.getOriginalFilename(), urlString);
				System.out.println("urlString[" + urlString + "]");
			}
		}
		return ResponseUtil.ok(data);
	}

}
